package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.ReportMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.ReportTempMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Report;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.ReportTemp;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.SomReportService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangcs on 2017/3/22.
 */
public class SomReportServiceImpl implements SomReportService {

    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private ReportTempMapper reportTempMapper;

    @Override
    public JSONObject list(Report report) {
        JSONObject jsonObject = new JSONObject();
        report.setLimit(report.getLimit() == null ? 10 : report.getLimit());
        report.setOffset(report.getOffset() == null ? 0 : report.getOffset());
        jsonObject.put("rows", JSON.toJSON(reportMapper.queryList(report)));
        jsonObject.put("total", reportMapper.queryCount(report));
        return jsonObject;
    }

    /**
     * 根据模板id查询最新的报表id
     *
     * @param reportTempId
     * @return
     */
    @Override
    public String queryReportIdByTempId(String reportTempId, String reportTime) {
        Map map = new HashMap<>();
        map.put("reportTempId", reportTempId);
        map.put("reportTime", reportTime);
        return reportMapper.queryReportIdByTempId(map);
    }

    /**
     * 根据组织和报表类型查询模板
     *
     * @param orgId
     * @param reportType
     * @return
     */
    @Override
    public List<ReportTemp> queryTempsByOrgAndType(String orgId, int reportType) {
        Map map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("reportType", reportType);
        return reportTempMapper.queryReportIdByTempId(map);
    }
}
